var gulp = require('gulp');
var cleanCss = require('gulp-clean-css')
var del = require('del');
var uglify = require('gulp-uglify')
var imagemin = require('gulp-imagemin')
var spriter = require('gulp-css-spriter')
var base64 = require('gulp-base64')
var browserSync = require('browser-sync').create();
var reload = browserSync.reload;
var rev = require('gulp-rev')
var revCollector = require('gulp-rev-collector');
var htmlmin = require('gulp-htmlmin');

var build = {
    css: './dist/css/',
    js: './dist/js/',
    images: './dist/images/',
    page: './dist/pages/',
    lib: {
        css: './dist/lib/css/',
        js: './dist/lib/js/',
        fonts: './dist/lib/fonts/'
    }
}

var src = {
    css: './css/',
    js: './js/',
    images: './images/',
    page: './pages/',
    lib: {
        css: './lib/css/',
        js: './lib/js/',
        fonts: './lib/fonts/'
    }
}

gulp.task('server:dev', function () {
    browserSync.init({
        server: {
            baseDir: "./",
            index: 'index.html' //将index的位置指向指定index.html
        },
        port: 8080
    });
    gulp.watch('pages/*.html', async () => {
        gulp.src(src.page + '*.html')
            .pipe(reload({
                stream: true
            }))
    })
    gulp.watch('*.html', async () => {
        gulp.src('*.html')
            .pipe(reload({
                stream: true
            }))
    })
    gulp.watch('css/*.css', async () => {
        gulp.src(src.css + '*.css')
            .pipe(reload({
                stream: true
            }))
    })
    gulp.watch('js/*.js', async () => {
        gulp.src(src.js + '*.js')
            .pipe(reload({
                stream: true
            }))
    })

});

/* ################################################# */

function deleteDist() {
    return del([
        './dist/'
    ])
}

function libmin() {
    return gulp.src('./lib/**/*')
        .pipe(gulp.dest('./dist/lib'))
}
function imagesmin() {
    return gulp.src(src.images + '*.*')
        .pipe(imagemin())
        .pipe(gulp.dest(build.images))
}
function publicCss() {
    return gulp.src(src.css + '*.css')
        // .pipe(spriter({
        //     'spriteSheet': build.images + 'spritesheet.png',
        //     'pathToSpriteSheetFromCSS': '../images/spritesheet.png'  //多张背景图片合并，减少请求
        // }))
        //    .pipe(base64())
        .pipe(cleanCss())
        .pipe(rev())
        .pipe(base64())
        .pipe(gulp.dest(build.css))
        .pipe(rev.manifest())
        .pipe(gulp.dest('./rev/css/'))
}
function publicJs() {
    return gulp.src(src.js + '*.js')
        .pipe(uglify())
        .pipe(rev())
        .pipe(gulp.dest(build.js))
        .pipe(rev.manifest())
        .pipe(gulp.dest('./rev/js/'))
}


function revBuild() {
    return gulp.src(['./rev/**/*.json', './*.html'])
        .pipe(revCollector({}))
        .pipe(htmlmin({ collapseWhitespace: true }))
        .pipe(gulp.dest('./dist/'))

}
function pageBuild() {
    return gulp.src(['./rev/**/*.json', src.page + '*.html'])
        .pipe(revCollector({}))
        .pipe(htmlmin({ collapseWhitespace: true }))
        .pipe(gulp.dest(build.page))
}
gulp.task('fileBuild', gulp.series(deleteDist, gulp.parallel(libmin, imagesmin, publicJs), publicCss, revBuild, pageBuild));

gulp.task('deleteAll', done => {
    del([
        './**/*'
    ])
    done();
})

gulp.task('default', done => {
    console.log('这是一条无效的默认命令')
    done();
})